﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;

namespace DatabaseCompare.Config
{

    /// <summary>
    /// 配置管理器.
    /// </summary>
    public class ConfigManager
    {

        ///	<summary>
        ///	配置文件名
        ///	</summary>
        private const string XML_FILE = "DbConfig.xml";

        ///	<summary>
        ///	DataTable。
        ///	</summary>
        private DataTable dataDt;

        private List<DbConfig> dbConfigList = new List<DbConfig>();

        private static ConfigManager me = new ConfigManager();


        private ConfigManager()
        {
            dataDt = new DataTable("DbConfig");
            // 数据库名
            dataDt.Columns.Add("DatabaseName");
            // 数据库类型
            dataDt.Columns.Add("DatabaseType");
            // 连接字符串.
            dataDt.Columns.Add("ConnectString");
        }


        /// <summary>
        /// 取得实例.
        /// </summary>
        /// <returns></returns>
        public static ConfigManager GetInstance()
        {
            return me;
        }


        public List<DbConfig> DatabaseConfig
        {
            get
            {
                return dbConfigList;
            }
        }


        ///	<summary>
        ///	读取配置信息.
        ///	</summary>
        ///	<returns></returns>
        public void ReadConfig()
        {
            dataDt.ReadXml(XML_FILE);

            dbConfigList.Clear();

            foreach (DataRow row in dataDt.Rows)
            {
                DbConfig config = new DbConfig();
                config.DatabaseName = row["DatabaseName"].ToString();
                config.DatabaseType = row["DatabaseType"].ToString();
                config.ConnectString = row["ConnectString"].ToString();
                dbConfigList.Add(config);
            }
        }

        ///	<summary>
        ///	写入配置信息
        ///	</summary>
        public void WriteConfig()
        {
            dataDt.Rows.Clear();

            foreach (DbConfig config in dbConfigList)
            {
                DataRow row = dataDt.NewRow();
                row["DatabaseName"] = config.DatabaseName;
                row["DatabaseType"] = config.DatabaseType;
                row["ConnectString"] = config.ConnectString;
                dataDt.Rows.Add(row);
            }

            dataDt.WriteXml(XML_FILE);
        }
    }




    public class DbConfig
    {
        /// <summary>
        /// 数据库名
        /// </summary>
        public string DatabaseName { set; get; }

        /// <summary>
        /// 数据库类型
        /// </summary>
        public string DatabaseType { set; get; }

        /// <summary>
        /// 连接字符串
        /// </summary>
        public string ConnectString { set; get; }
    }


}
